package com.example.springdemo.service;

import java.util.List;

import com.example.springdemo.entity.Order;
import com.example.springdemo.mapper.OrderMapper;
import com.example.springdemo.mapper.OrderdetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * orderService业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-07
 */
@Service
public class OrderServiceImpl implements IOrderService 
{
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderdetailMapper orderdetailMapper;

    /**
     * 查询order
     * 
     * @param orderid order主键
     * @return order
     */
    @Override
    public Order selectOrderByOrderid(Long orderid)
    {
        return orderMapper.selectOrderByOrderid(orderid);
    }

    @Override
    public Order getOrderByOrdersn(String ordersn) {
        return orderMapper.getOrderByOrdersn(ordersn);
    }

    /**
     * 查询order列表
     * 
     * @param order order
     * @return order
     */
    @Override
    public List<Order> selectOrderList(Order order)
    {
        return orderMapper.selectOrderList(order);
    }







    /**
     * 新增order
     * 
     * @param order order
     * @return 结果
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout = 6000,rollbackFor = Exception.class)
    @Override
    public int insertOrder(Order order)
    {
        int i = orderMapper.insertOrder(order); //步骤1
//        //抛出异常
//        int a = 1/0;  //埋的错误

        if(i>0){
            int j = orderdetailMapper.insertOrderdetails(order.getOrderdetailList());
            if(j>0){
                return 1;
            }else{
                return 0;
            }
        }else {
            return 0;
        }
    }

    /**
     * 修改order
     * 
     * @param order order
     * @return 结果
     */
    @Override
    public int updateOrder(Order order)
    {
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除order
     * 
     * @param orderids 需要删除的order主键
     * @return 结果
     */
    @Override
    public int deleteOrderByOrderids(Long[] orderids)
    {
        return orderMapper.deleteOrderByOrderids(orderids);
    }

    /**
     * 删除order信息
     * 
     * @param orderid order主键
     * @return 结果
     */
    @Override
    public int deleteOrderByOrderid(Long orderid)
    {
        return orderMapper.deleteOrderByOrderid(orderid);
    }
}
